package com.nengjiang.wsplayer.base;

import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.nengjiang.wsplayer.R;
import com.nengjiang.wsplayer.activity.LoginActivity;
import com.nengjiang.wsplayer.utils.StatusBarUtil;
import com.nengjiang.wsplayer.utils.SystemUtils;


/**
 * 自定义title status
 */
public class BaseThemeActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//        }
        if (null != getSupportActionBar()) {
            getSupportActionBar().setElevation(0);
            getSupportActionBar().hide();
        }
        if (noStatusBar()) {
            StatusBarUtil.setStatusBarFullTransparent(this);
        } else {
            setStatusBarColorByStatus();
        }
        StatusBarUtil.StatusBarLightMode(this);
        setDisplayHomeAsUpEnabled(true);
//        setBackgroundDrawable(R.drawable.bg_line);
//        setHomeAsUpIndicator(R.mipmap.return_back);
    }

    /**
     * 是否显示statusBar，一般情况都显示，只有在照片覆盖整个屏幕这种情况需求隐藏statusBar
     * 其他情况下都是修改statusBar的颜色以达到沉浸式状态栏
     *
     * @return
     */
    protected boolean noStatusBar() {
        return false;
    }

    /**
     * 根据不同的状态设置不同的statusBar的颜色
     */
    public void setStatusBarColorByStatus() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            View decorView = getWindow().getDecorView();
            setDecorViewPaddingTop(SystemUtils.getStatusBarHeight(this));
            int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
            decorView.setSystemUiVisibility(option);
            StatusBarUtil.setStatusBarColor(this, setStatusBarColor());
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
    }

    /**
     * 重写该方法修改statusBar颜色
     *
     * @return
     */
    protected int setStatusBarColor() {
        return R.color.white;
    }

    private void setDecorViewPaddingTop(int padNum) {
        ViewGroup rootView = getWindow().getDecorView().findViewById(android.R.id.content);
        rootView.setPadding(0, padNum, 0, 0);
    }

    protected void setDisplayHomeAsUpEnabled(Boolean enabled) {
        if (null != getSupportActionBar())
            getSupportActionBar().setDisplayHomeAsUpEnabled(enabled);
    }

    protected void setBackgroundDrawable(int id) {
        if (null != getSupportActionBar())
            getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(id));
    }

    protected void setHomeAsUpIndicator(int id) {
        if (null != getSupportActionBar())
            getSupportActionBar().setHomeAsUpIndicator(id);
    }


    private Dialog dialog;

    @Override
    protected void onDestroy() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
        dialog = null;
        super.onDestroy();
    }

    protected void showToast(String message) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(BaseThemeActivity.this, message, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
